package com.hthy.futures.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.zxing.WriterException;
import com.hthy.futures.AppContext;
import com.hthy.futures.R;
import com.hthy.futures.base.BaseActivity;
import com.hthy.futures.utils.ImageUtils;
import com.hthy.futures.utils.QrCodeUtils;
import com.hthy.futures.view.TitleView;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;

import java.io.File;
import java.io.IOException;
import java.util.List;

import butterknife.Bind;

/**
 * 分享
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/5/8
 */

public class ShareActivity extends BaseActivity {
    @Bind(R.id.iv_qr_code)
    ImageView mIvScanCode;
    @Bind(R.id.titleview)
    TitleView mTitleView;
    private Bitmap mBitmap;

    @Bind(R.id.ll_share)
    LinearLayout mLlShare;


    @Override
    protected int getContentView() {
        return R.layout.act_share;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTitleView.setTitle(getString(R.string.share));
        mTitleView.setBackBtn();
        // 生成二维码
        try {
            String url = AppContext.shareIp + "?ref_user=" + AppContext.account;
            mBitmap = QrCodeUtils.Create2DCode(url);
            mIvScanCode.setImageBitmap(mBitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }


        final File appDir = new File(Environment.getExternalStorageDirectory() + "/hthy");
        final File copyUrl = new File(appDir.getAbsolutePath() + "/" + String.valueOf(System.currentTimeMillis()) + ".jpg");

        mLlShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Acp.getInstance(ShareActivity.this).request(new AcpOptions.Builder()
                                .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                .build(),
                        new AcpListener() {
                            @Override
                            public void onGranted() {
                            }

                            @Override
                            public void onDenied(List<String> permissions) {
                                //showToast(permissions.toString() + "权限拒绝");
                            }
                        });
                try {
                    ImageUtils.saveImageToSD(ShareActivity.this, copyUrl.getAbsolutePath(), mBitmap, 100);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                shareImg(copyUrl);
            }
        });

    }

    /**
     * 调用系统分享
     *
     * @param copyUrl
     */
    private void shareImg(File copyUrl) {
        Uri imageUri = Uri.fromFile(new File(copyUrl.getAbsolutePath()));
        Log.d("share", "uri:" + imageUri);  //输出：file:///storage/emulated/0/test.jpg

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

}
